package com.erp.activity

import android.content.Intent
import androidx.appcompat.app.AlertDialog
import com.erp.Interface.OnUserDataInterface
import com.erp.Myapp
import com.erp.R
import com.erp.permission.PermissionListener
import com.erp.util.Httputil
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import com.erp.util.Sputil
import java.io.File

/**
 * Created by Administrator on 2016/12/14/014.
 * 作者：刘洋
 */
class LoadActivity : BaseActivity() {

    private var islogin = false//登录状态，true为已登录
    lateinit var dialog: AlertDialog
    private val a = "LoadActivity"
    override fun getLayoutId(): Int {
        activityname = a;
        return R.layout.activity_load
    }
//
    override fun initCreate() {
        islogin = sp.getBoolean("islogin", islogin)
        PermissionUtils.isupdate(this@LoadActivity, object : PermissionListener {
            override fun onSucceed(requestCode: Int, grantPermissions: List<String>) {
                putError()
                isdenglu()
            }

            override fun onFailed(requestCode: Int, deniedPermissions: List<String>){
                isdenglu()
            }
        })
    }

    private fun putError() {
        if (!sp.getBoolean("errorok", false) && File(sp.getString("error", "")).exists()) {
            editor.putBoolean("errorok", true)
            editor.commit()
            Httputil.updataError(Myapp.getContext(), Sputil.sp("erp").getString("uid", ""), sp.getString("error", ""), object : OnUserDataInterface {
                override fun onUserData(data: String) {
                }

                override fun onNot(throwable: String) {
                }
            })
        }
    }
    fun isdenglu() = if (islogin) {
        intohttp()//判断登录状态
    } else {
        val intent = Intent(this@LoadActivity, LoginActivity::class.java)
        startActivity(intent)
        finish()
    }
    var iaa = false
    //获取用户数据的网络访问
    private fun intohttp() {
        dialog = Myutil.showupdatedialog(this, "获取用户信息", false)
        Httputil.gethttpuser(this, sp.getString("uid", ""), object : OnUserDataInterface {
            override fun onUserData(data: String) {
                dialog.dismiss()
                editor.putString("user", data)
                editor.commit()
                iaa = true
                val intent = Intent(this@LoadActivity, NewMainKActivity::class.java)
                startActivity(intent)
                finish()
            }

            override fun onNot(throwable: String) {
                dialog.dismiss()
                if (throwable.indexOf("UnknownHostException") != -1) {
                    Myutil.MyTost("无网络")
                    val intent = Intent(this@LoadActivity, LoginActivity::class.java)
                    startActivity(intent)
                    finish()
                } else if(throwable.indexOf("Access denied for user anonymous") != -1){
                    editor.putBoolean("islogin", false)
                    editor.commit()
                    finish()
                }else{
                    val intent = Intent(this@LoadActivity, LoginActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            }
        })
    }

}
